package vn.edu.hungvuongaptech.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.ProcessDAO;
import vn.edu.hungvuongaptech.dao.ProductDAO;
import vn.edu.hungvuongaptech.dao.ProjectDAO;
import vn.edu.hungvuongaptech.dao.StatusDAO;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;
import vn.edu.hungvuongaptech.dao.UserDAO;
import vn.edu.hungvuongaptech.dao.WorkDAO;
import vn.edu.hungvuongaptech.model.ProcessModel;
import vn.edu.hungvuongaptech.model.ProductModel;
import vn.edu.hungvuongaptech.model.ProjectModel;
import vn.edu.hungvuongaptech.model.StatusModel;
import vn.edu.hungvuongaptech.model.TimeSheetListModel;
import vn.edu.hungvuongaptech.model.TimeSheetModel;
import vn.edu.hungvuongaptech.model.UserModel;
import vn.edu.hungvuongaptech.model.WorkModel;

/**
 * Servlet implementation class AddTimeSheetController
 */
public class AddTimeSheetController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddTimeSheetController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);		
		String strURL = "";		
		String pageNext = "";
		ArrayList<TimeSheetModel> timeSheetList = new ArrayList<TimeSheetModel>();
		ArrayList<TimeSheetModel> timeSheetRejectList = new ArrayList<TimeSheetModel>();
		
		for (int i = 1; i < Integer.parseInt(Constant.SETTING_RES.getString("ADD_TIMESHEETS.NUMBERS_OF_RECORDS")); i++) {
			try {
				long userId = Long.parseLong(session.getAttribute(
						Constant.USERID_ATT).toString());
				long projectId = Long.parseLong(request.getParameter(
						"cboProject" + i).toString());
				int productId = Integer.parseInt(request.getParameter(
						"cboProduct" + i).toString());
				int processId = Integer.parseInt(request.getParameter(
						"cboProcess" + i).toString());
				int workId = Integer.parseInt(request.getParameter(
						"cboWork" + i).toString());
				String date = request.getParameter("txtDate" + i);
				Double time = Double.parseDouble(request.getParameter(
						"txtTime" + i).toString());
				String description = request.getParameter("txtDescription" + i);
				
				ProductModel productModel = ProductDAO.getProductByID(productId);
				ProcessModel processModel = ProcessDAO.getProcessByID(processId);
				ProjectModel projectModel = ProjectDAO.getProjectByID(projectId);
				WorkModel workModel = WorkDAO.getWorkByID(workId);
				UserModel userModel = UserDAO.getUserByUserId(userId);
				StatusModel statusModel = StatusDAO.getStatusById(1); // 1:Pending 2:Approve 3:Reject
				
				TimeSheetModel timeSheetModel = new TimeSheetModel(-1, date,
						time, description, productModel, workModel,
						processModel, userModel, statusModel, projectModel);
				timeSheetList.add(timeSheetModel);
			} catch (Exception e) {
				// TODO: handle exception
				continue;
			}
		}
		
		TimeSheetListModel timeSheetListModel = new TimeSheetListModel(timeSheetList);
		timeSheetListModel.rejectRecordInvalid(timeSheetRejectList);
		
		if (timeSheetListModel.getTimeSheetList().size() > 0) {
			TimeSheetDAO.insertTimeSheetList(timeSheetListModel.getTimeSheetList());
			strURL += "?msg= " 
						+ timeSheetListModel.getTimeSheetList().size() + " "
						+ Constant.MESSAGE_RES.getString("timesheet.mesage.addSuccess");	
			if (timeSheetRejectList.size() > 0) {
				strURL += "&&err= " + timeSheetRejectList.size() + " "
							+ Constant.MESSAGE_RES.getString("timesheet.mesage.addRejectData");
				request.setAttribute("TimeSheetRejectList", timeSheetRejectList);				
			}
		} else {
			if (timeSheetRejectList.size() > 0) {
				strURL += "?err= "  + timeSheetRejectList.size() + " "
									+ Constant.MESSAGE_RES.getString("timesheet.mesage.addRejectData");
			} else {
				strURL += "?err= "  + Constant.MESSAGE_RES.getString("timesheet.mesage.addError");
									
			}
			request.setAttribute("TimeSheetRejectList", timeSheetRejectList);
		}		
		
		if (timeSheetRejectList.size() > 0) { // Case: contain record invalid
			pageNext = Constant.PATH_RES.getString("timesheet.staff.addTimeSheetCurrentPath") + strURL;
		} else { // Case: all record had valid
			pageNext = Constant.PATH_RES.getString("timesheet.staff.listTimeSheetCurrentPath") + strURL;
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(pageNext);
		rd.forward(request, response);			
	}
	

}
